package edu.hawaii.ics211.exam;

/**
 * Various methods for grading an exam question.
 * 
 * @author Branden Ogata
 *
 */

public class ExamUtility
{
  /**
   * Counts the number of passing tests.
   * A passing test should print out "PASS".
   * 
   * @param result The String containing the results for the question.
   * 
   * @return passes The int containing the number of passing tests for the question.
   * 
   */
  
  public static int countPasses(String result)
  {
    int passes = 0;
    int index = 0;
    
    // Until reaching the end of the parameter String, keep on searching for "PASS"
    while (index < result.length())
    {
      if (result.substring(index).startsWith("PASS"))
      {
        passes++;
      }
      
      index++;
    }
    
    return passes;
  }
  
  /**
   * Counts the number of failing tests.
   * A passing test should print out "FAIL".
   * 
   * @param result The String containing the results for the question.
   * 
   * @return fails The int containing the number of failing tests for the question.
   * 
   */
  
  public static int countFails(String result)
  {
    int fails = 0;
    int index = 0;
    
    // Until reaching the end of the parameter String, keep on searching for "FAIL"
    while (index < result.length())
    {
      if (result.substring(index).startsWith("FAIL"))
      {
        fails++;
      }
      
      index++;
    }
    
    return fails;
  }
  
  /**
   * Counts the total number of tests.
   * 
   * @param result The String containing the results for the question.
   * 
   * @return The int containing the number of tests for the question.
   * 
   */
  
  public static int countTests(String result)
  {
    return ExamUtility.countPasses(result) + ExamUtility.countFails(result);
  }
}
